package handler

import (
	"context"
	"fmt"
	"rpcSrv/configs"
	"rpcSrv/dbs/mysqlGORM"
	"rpcSrv/entitys"
	pb "rpcSrv/proto/user"
	"rpcSrv/utils"
	"time"
)

type UserHdl struct {
}

func (this *UserHdl) UsernameLogin(ctx context.Context, req *pb.LogRegParams) (resp *pb.RespUserParams, err error) {
	username := req.GetUsername()
	password := req.GetPassword()
	user := new(entitys.User)
	db, err := mysqlGORM.GetMysqlDB()
	if utils.CheckErr(err) {
		return nil, err
	}
	if notFound := db.Table(user.TableName()).
		Select("*").
		Where("username = ?", username).
		Offset(0).Limit(1).Scan(user).
		RecordNotFound(); notFound {
		return nil, fmt.Errorf("用户名不存在")
	}
	password_sha1 := utils.GetShe1Base64ByKey(configs.GetSha1KeyConf(), []byte(password))
	if password_sha1 != user.Password {
		return nil, fmt.Errorf("用户密码错误")
	}
	if err = db.Table(user.TableName()).
		Where("id = ?", user.ID).
		UpdateColumn("updated_timestamp", time.Now().Unix()).
		Error; utils.CheckErr(err) {
		return nil, err
	}
	resp = new(pb.RespUserParams)
	db.Table(fmt.Sprintf("%v as a",new(entitys.User).TableName())).
		Joins(fmt.Sprintf("left join %v as b on a.cover=b.id",new(entitys.FileCrypto).TableName())).
		Select("a.id,a.username,a.phone,a.email,b.file_path as cover,a.nickname,a.head_image,a.created_timestamp,a.updated_timestamp,case when a.vip_expire>"+fmt.Sprintf("%v", time.Now().Unix())+" then 1 else 0 end as vip").
		Where("a.id = ?", user.ID).
		Limit(1).
		Scan(resp)
	return resp, nil
}

func (this *UserHdl) UsernameRegister(ctx context.Context, req *pb.LogRegParams) (resp *pb.RespUserParams, err error) {
	username := req.GetUsername()
	password := req.GetPassword()
	password_sha1 := utils.GetShe1Base64ByKey(configs.GetSha1KeyConf(), []byte(password))
	db, err := mysqlGORM.GetMysqlDB()
	if utils.CheckErr(err) {
		return nil, err
	}
	n := 0
	user := new(entitys.User)
	if err = db.Table(user.TableName()).Where("username = ?", username).Count(&n).Error; utils.CheckErr(err) {
		return nil, err
	}
	if n > 0 {
		return nil, fmt.Errorf("用户名已存在")
	}
	user.Username = username
	user.Password = password_sha1
	user.Nickname = username
	user.HeadImage = "https://zdsicons.oss-cn-qingdao.aliyuncs.com/favicon.ico"
	user.CreatedTimestamp = time.Now().Unix()
	user.UpdatedTimestamp = time.Now().Unix()
	tx := db.Begin()
	if err = tx.Table(user.TableName()).Create(user).Error; utils.CheckErr(err) {
		return nil, err
	}
	resp = new(pb.RespUserParams)
	db.Table(fmt.Sprintf("%v as a",new(entitys.User).TableName())).
		Joins(fmt.Sprintf("left join %v as b on a.cover=b.id",new(entitys.FileCrypto).TableName())).
		Select("a.id,a.username,a.phone,a.email,b.file_path as cover,a.nickname,a.head_image,a.created_timestamp,a.updated_timestamp,case when a.vip_expire>"+fmt.Sprintf("%v", time.Now().Unix())+" then 1 else 0 end as vip").
		Where("a.id = ?", user.ID).
		Limit(1).
		Scan(resp)
	return resp, nil
}

func (this *UserHdl) UpdateUserInfo(ctx context.Context, req *pb.UpdateUserParams) (resp *pb.RespUserParams, err error) {
	return nil, nil
}
